package com.well.menjin.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.well.menjin.domain.Zone;
import com.well.menjin.model.Sort;
import com.well.menjin.model.ZoneOpt;
import com.well.menjin.service.IZoneService;



/**
 * 
* @ClassName: ZoneController 
* @author 姜俊健 
* @date 2016年6月13日 下午4:26:08 
*
 */
@Controller
@RequestMapping("/zone")
public class ZoneController extends BaseController {
	
	

	
	@Autowired
	private IZoneService zoneservice;
	
	/**
	 * 
	* @Title: listZones 
	* @Description: 列出所有区域
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value="/listZones",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String listZones(@RequestParam(value="start",required=false) int start,
			@RequestParam(value="limit",required=false) int limit,
			@RequestParam(value="page",required=false) int page,
			@RequestParam(value="sort",required=false) String sorts){
		if(sorts!=null){
			sorts = sorts.replace("]", "");
			sorts = sorts.replace("[", "");
			sorts.trim();
		}
		Sort sorto = JSON.parseObject(sorts,Sort.class);
		List<Zone> zones = zoneservice.getZones(start,limit,page,sorto);
		int totalCount = zoneservice.getZonesCount();
		return jsonBuilder.buildObjListToJson(Long.valueOf(totalCount), zones, true);
	}
	
	/**
	 * 
	* @Title: isExist 
	* @Description: 判断一个区域是否存在 
	* @return void    返回类型 
	* @throws
	 */
	@RequestMapping(value="/isExist",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String isExist(Zone zone){
		boolean result = zoneservice.isExist(zone.getZone_no());
		if (result) {
			return jsonBuilder.returnSuccessJson("该区域编号不重复");
		}else{
			return   jsonBuilder.returnFailureJson("该区域编号重复");
		}
	}
	
	/**
	 * 
	* @Title: addZone 
	* @Description: 增加区域 
	* @return void    返回类型 
	* @throws
	 */
	@RequestMapping(value= "/addZone",produces = "text/html;charset=UTF-8")
	@ResponseBody
	public String addZone(Zone zone){
		boolean result = zoneservice.addZone(zone); 
		if (result) {
			 return jsonBuilder.returnSuccessJson("添加区域成功");
		}else {
			return  jsonBuilder.returnFailureJson("添加区域失败");
		}
		
	}
	
	/**
	 * 
	* @Title: updateZone 
	* @Description: 修改区域 
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value= "/updateZone",produces ="text/html; charset=UTF-8")
	@ResponseBody
	public String updateZone(Zone zone){
		boolean result = zoneservice.updateZone(zone);
		if (result) {
			 return jsonBuilder.returnSuccessJson("修改区域成功");
		}else {
			return  jsonBuilder.returnFailureJson("修改区域失败");
		}
	}
	
	/**
	 * \
	* @Title: listZonesOpts 
	* @Description: 列出用于在Combobox显示的信息
	* @return String    返回类型 
	* @throws
	 */
	@RequestMapping(value= "/listZonesOpts",produces ="text/html; charset=UTF-8")
	@ResponseBody
	 public String listZonesOpts(){
		List<ZoneOpt> zoneOpts = zoneservice.getZonesOpts();
		int totalCount = zoneservice.getZonesCount();
		return jsonBuilder.buildObjListToJson(Long.valueOf(totalCount), zoneOpts, true);
	}
	
}
